package cn.ynu.cmrs.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.ynu.cmrs.R;
import cn.ynu.cmrs.activity.CourseDetailActivity;
import cn.ynu.cmrs.asyncTask.RemoveFavoriteTask;
import cn.ynu.cmrs.model.CourseInfoItem;
import cn.ynu.cmrs.utils.VideoUtil;
import cn.ynu.cmrs.widget.RemoteImageView;

/**
 * Adapter representing favorite course
 * 
 * @author Yasha
 * 
 */
public class FavoriteAdapter extends ArrayListAdapter<CourseInfoItem> {

	protected static final int IMAGE = 1;
	protected static final int LYRIGHT = 2;
	protected static final int LYMIDDLE = 3;

	public FavoriteAdapter(Activity context) {
		super(context);
	}

	public FavoriteAdapter(Activity context, ArrayList<CourseInfoItem> course,
			ListView listView) {
		super(context);
		this.mList = course;
		this.mListView = listView;
	}

	@Override
	public long getItemId(int position) {
		if (position == getCount() - 1) {
			return -1;
		} else {
			return Long.parseLong(mList.get(position).getId());
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row;

		if (position == this.getCount() - 1)// see more
		{
			TextView tv;
			row = LayoutInflater.from(mContext).inflate(R.layout.moreitemsview,
					null);
			tv = (TextView) row.findViewById(R.id.tvItemContent);

			tv.setText(R.string.lookMore);
			return row;
		} else { // course item
			ViewHolder holder;
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.favorite_item, null);

			holder = new ViewHolder();
			holder.courseImage = (RemoteImageView) row
					.findViewById(R.id.ivItemCourseImage);
			holder.playButton = (ImageView) row
					.findViewById(R.id.ivItemPlayButton);
			holder.courseNameText = (TextView) row
					.findViewById(R.id.tvItemName);
			holder.teacherText = (TextView) row
					.findViewById(R.id.tvItemTeacherName);
			holder.lyRight = row.findViewById(R.id.lyRightLayout);
			holder.lyRight.setTag(LYRIGHT);
			holder.lyMiddle = row.findViewById(R.id.lyMiddleLayout);
			holder.lyMiddle.setTag(LYMIDDLE);
			holder.courseImage.setTag(IMAGE);
			holder.courseImage.setDefaultImage(R.drawable.course_default_icon);
			holder.courseImage.setImageUrl(mList.get(position).getIcon(),
					position, getListView());
			if(mList.get(position).getVideo() != null && mList.get(position).getVideo().length > 0)
			{
				holder.playButton.setVisibility(View.VISIBLE);
			}
			holder.courseNameText.setText(mList.get(position).getName());
			holder.teacherText.setText(mList.get(position).getTeacher());
			setListener(holder.courseImage, position);
			setListener(holder.lyMiddle, position);
			setListener(holder.lyRight, position);
			return row;
		}
	}

	private void setListener(View view, int position) {
		// TODO Auto-generated method stub
		int tag = (Integer) view.getTag();
		switch (tag) {
		case IMAGE:// play video
			final String[] video = mList.get(position).getVideo();
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					VideoUtil.playVideoFromUrl(video, mContext);
				}
			});
			break;

		case LYMIDDLE: // view detail course information
			final CourseInfoItem cis = mList.get(position);
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent it = new Intent();
					it.setClass(mContext, CourseDetailActivity.class);
					it.putExtra("CourseInfoItem", cis);
					mContext.startActivityForResult(it, 105);
				}
			});
			break;

		case LYRIGHT: // remove from favorite
			final String cid = mList.get(position).getId();
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					new RemoveFavoriteTask(mContext,
							R.string.remove_favorite_success,
							R.string.remove_favorite_fail).execute(cid);
				}
			});
			break;
		}
	}

	/**
	 * Add more data to adapter
	 * 
	 * @param items
	 */
	public void addMoreData(ArrayList<CourseInfoItem> items) {
		this.mList.addAll(items);
		this.notifyDataSetChanged();
	}

	/**
	 * Class implementing holder pattern, performance favorite course
	 * 
	 * @author Yasha
	 * 
	 */
	static class ViewHolder {
		RemoteImageView courseImage;
		ImageView playButton;
		TextView courseNameText;
		TextView teacherText;
		View lyMiddle;
		View lyRight;
	}

}
